var express = require('express');
var router = express.Router();
const schedule = require('node-schedule')

const { By, Builder, Key, until } = require('selenium-webdriver');

const list = [{ 
    'xh': '1810609215',
    'mm': '200415',
    'dq': '北碚'
}, { 
    'xh': '1810609227',
    'mm': '164529',
    'dq': '长寿'
}, { 
    'xh': '1810609214',
    'mm': '160011',
    'dq': '沙坪坝'
}]

// console.log(list.length)

const scheduleCronstyle = () => {
    //每分钟的第30秒定时执行一次:
    schedule.scheduleJob('50 32 21 * * *', () => {
        console.log('scheduleCronstyle:' + new Date());
        handleBaiDuDriver()
    });
}
const handleBaiDuDriver = async () => {
    for (let i = 0; i < list.length; i++) {
        let driver = await new Builder().forBrowser('chrome').build();
        try {
            await driver.get('http://i.cque.edu.cn/cas/login?service=http://xsc.cque.edu.cn:866/Mobile/login.aspx');
            await driver.findElement(By.id('username')).sendKeys(list[i].xh);//正常使用
            await driver.findElement(By.id('password')).sendKeys(list[i].mm, Key.RETURN);//正常使用
            await driver.wait(until.titleIs('奥蓝学生管理信息系统(学生版)'), 2000);
            await driver.get('http://xsc.cque.edu.cn:866/Mobile/rsbulid/r_3_3_st_xgfkmrdk4.aspx');
            await driver.findElement(By.tagName('img')).click();
    
            await driver.wait(until.titleIs('新冠防控每日打卡4'), 2000);
            await driver.findElement(By.id('sfzxx')).click();
            await driver.switchTo().frame(driver.findElement(By.id('r_xzdm')))
            await driver.findElement(By.id('mh')).sendKeys('否');//正常使用
            await driver.findElement(By.id('cx')).click();
            await driver.sleep(2000);
            await driver.findElement(By.id('MyDataGrid__ctl3_mc')).click();
            await driver.switchTo().defaultContent()
    
            await driver.wait(until.titleIs('新冠防控每日打卡4'), 2000);
            await driver.findElement(By.id('jrszd1')).click();
            await driver.switchTo().frame(driver.findElement(By.id('r_xzdm')))
            await driver.findElement(By.id('mh')).sendKeys(list[i].dq);//正常使用
            await driver.findElement(By.id('cx')).click();
            await driver.sleep(2000);
            await driver.findElement(By.id('MyDataGrid__ctl3_mc')).click();
            await driver.switchTo().defaultContent()
    
            await driver.findElement(By.id('tw_cj')).clear();
            await driver.findElement(By.id('tw_wj')).clear();
            await driver.findElement(By.id('tw_cj')).sendKeys('36.2');//正常使用
            await driver.findElement(By.id('tw_wj')).sendKeys('36.4');//正常使用
            await driver.findElement(By.name('databc')).click();
        } catch (error) {
            console.log(error)
        } finally {
            await driver.sleep(2000);
            await driver.quit();
            await driver.sleep(30000);
        }
    }
}

// scheduleCronstyle();

// handleBaiDuDriver()

router.get('/', async function (req, res, next) {
    const title = await handleBaiDuDriver();
    res.send({ title });
});

module.exports = router;
